﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InterrogazioneWebServicesBPM
{
    public partial class BpmSecurityContext : Form
    {
        public BpmSecurityContext()
        {
            InitializeComponent();
            
        }  
               

        private void confirm_Click(object sender, EventArgs e)
        {
            //al click devo invocare il servizio per caricare le informazioni del caso.         

            if ((textBoxUid == null) || ("".Equals(textBoxUid.Text.Trim())))
            {
                System.Windows.Forms.MessageBox.Show("E' necessario speficare lo >> User ID << ", "Attenzione!",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Warning);
            }
            else {
                textBoxResponse.Text = this.userToString();
            }
        }

        private void resetButton_Click(object sender, EventArgs e)
        {
            //resetto tutto
            textBoxResponse.Text = "";
            textBoxUid.Text = "";
            comboBoxApplicationChoosen.SelectedIndex = 0;
            comboBoxChooseRealm.SelectedIndex = 0;
        }

        private void comboBoxChooseRealm_SelectedIndexChanged(object sender, EventArgs e)
        {
            //entro nella selezione e quindi prendo quello selezionato
        }

        private static string CR = "\r\n";

        private string userToString() {
            Cursor.Current = Cursors.WaitCursor;
            StringBuilder userDb = new StringBuilder();
            string applChoosen = (comboBoxApplicationChoosen.SelectedItem != null)?comboBoxApplicationChoosen.SelectedItem.ToString():"PTL0001";
            if ("SAS".Equals(applChoosen))
            { 
                //default
                applChoosen = "SAS0001";
            }
            LoadBpmSecurityContextBusinessDelegate lbd = new LoadBpmSecurityContextBusinessDelegate("", applChoosen, "BPM", "SICUREZZA", "SICUREZZA");
            string realm = (comboBoxChooseRealm.SelectedItem != null)?comboBoxChooseRealm.SelectedItem.ToString():"BPM";
            LoadBpmSecurityContextWS.User user = lbd.getSecurityContext(textBoxUid.Text, realm );
            if (user != null)
            {
                userDb.Append(user.name).Append(" ").Append(user.surname).Append(CR);
                userDb.Append("UID=").Append(user.userId).Append(CR);
                userDb.Append("OU/CAB/ABI=").Append(user.orgUnit.code).Append("/").Append(("".Equals(user.orgUnit.cab.Trim())?"N.A.":user.orgUnit.cab)).Append("/").Append(user.realm.abi).Append(CR);
                userDb.Append("CANALE=").Append(user.channel).Append(CR);
                userDb.Append("Responsabile?").Append((user.responsible) ? "SI" : "NO").Append(CR);
                userDb.Append("Esterno?").Append((user.guest) ? "SI" : "NO").Append(CR);
                //banche abilatate
                if (user.realmsAvailable != null)
                {
                    foreach (LoadBpmSecurityContextWS.Realm banca in user.realmsAvailable)
                    {
                        userDb.Append("Banche Abilitate=").Append(banca.code).Append(" "); 
                    }
                    userDb.Append(CR);
                }
                //ruoli
                if (user.role != null) {                  
                    foreach (LoadBpmSecurityContextWS.Role ruolo in user.role) {
                        userDb.Append("Ruoli=").Append(ruolo.name).Append(" ");
                    }
                    userDb.Append(CR);
                }
                //portafogli
                if (user.wallet!=null){
                    if (user.wallet.Length > 0)
                    {
                        foreach (LoadBpmSecurityContextWS.Wallet ptf in user.wallet)
                        {
                            userDb.Append("PTF=").Append(ptf.code).Append("(").Append((ptf.main) ? "P." : "N.P.").Append(")");
                        }
                        userDb.Append(CR);
                    }
                }
            }
            else
                userDb.Append("unknown");
            Cursor.Current = Cursors.Default;
            return userDb.ToString();

        }

       
               
    }
}
